//
//  HYYStatusOriginalView.swift
//  Hyy9Weibo
//
//  Created by Air on 16/4/3.
//  Copyright © 2016年 hyy. All rights reserved.
//

import UIKit

/// 原创微博View
class HYYStatusOriginalView: UIView {
    
    //MARK: - 属性
    /// 微博模型
    var status: HYYStatus?{
        didSet{
            //当别人来设置微博模型的时候，我们根据微博模型来设置原创微博的相关内容
            //1.设置头像
            if let imageUrl = status?.user?.profile_image_url{
                let url = NSURL(string: imageUrl)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //设置名称
            nameLabel.text = status?.user?.screen_name
            //设置时间
            timeLabel.text = status?.created_at
            //来源 
            sourceLabel.text = status?.source
            //会员等级
            if let mbrank = status?.user?.mbrank{
                if mbrank > 0 && mbrank <= 6{
                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
                }else{
                    //防止cell复用，没有会员的就不要等级
                    memberView.image = nil
                }
            }
            //认证图标
            if let verified_type = status?.user?.verified_type{
                var image:UIImage?
                switch verified_type {
                case 0:
                    image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    image = UIImage(named: "avatar_grassroot")
                default:
                    image = nil
                }
                verifiedView.image = image
            }
            //设置微博内容
            contentLabel.text = status?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor.whiteColor()
        prepareUI()
    }
    
    private func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        //添加约束
        //用户头像
        iconView.snp_makeConstraints { (make) in
            make.leading.equalTo(self).offset(HYYStatusCellMargin)
            make.top.equalTo(self).offset(HYYStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //用户名称
        nameLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView.snp_trailing).offset(HYYStatusCellMargin)
            make.top.equalTo(iconView)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(iconView.snp_trailing).offset(HYYStatusCellMargin)
            make.bottom.equalTo(iconView)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HYYStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        //会员等级
        memberView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(HYYStatusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        //认证图标
        verifiedView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        //配图视图
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom)
            make.leading.equalTo(self).offset(HYYStatusCellMargin)
            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        //微博文本内容
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(self.snp_leading).offset(HYYStatusCellMargin)
            make.trailing.equalTo(self.snp_trailing).offset(-HYYStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(HYYStatusCellMargin)
        }
        //原创cell底部参照配图底部
        self.snp_makeConstraints { (make) in
            make.bottom.equalTo(pictureView.snp_bottom).offset(HYYStatusCellMargin)
        }
    }
    
    //MARK: - 懒加载
    /**
     *  用户头像
     */
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar"))
    /**
     *  用户名称
     */
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
//        label.text = "科科"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(HYYStatusCellContenSize)
        
        return label
    }()
    /**
     *  来源
     */
    private lazy var sourceLabel:UILabel = {
        let label = UILabel()
//        label.text = "iPhone 7"
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(HYYStatusCellButtonDefaultFontSize)
        return label
    }()
    /**
     *  时间
     */
    private lazy var timeLabel:UILabel = {
       let label = UILabel()
//        label.text = "刚刚"
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(HYYStatusCellButtonDefaultFontSize)
        return label
    }()
    /**
     *  会员等级
     */
    private lazy var memberView:UIImageView = UIImageView()
    
    /**
     *  认证图标
     */
    private lazy var verifiedView:UIImageView = UIImageView()
    /**
     *  原创微博内容
     */
    private lazy var contentLabel:UILabel = {
        let label = UILabel()
//        let count = arc4random_uniform(2)
//        label.text = count == 0 ? "666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666" : "9999999"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(HYYStatusCellContenSize)
        label.numberOfLines = 0
        return label
    }()
    
    /**
     *  配图视图,不管cell是否需要显示图片，先加载进来，如果模型里不需要显示图片，就把这个配图隐藏掉，如果要显示就把图片显示出来
     */
    private lazy var pictureView:HYYStatusPictureView = HYYStatusPictureView()
}
